package com.iot.service;

import com.iot.mapper.*;
import com.iot.pojo.MRef;
import com.iot.pojo.MTapp;
import com.iot.pojo.MTarea;
import com.iot.pojo.MTusers;
import com.iot.util.BaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TappServiceImpl extends BaseServiceImpl<MTapp> implements TappService {
    @Autowired
    private TappMapper mapper;
    @Autowired
    private DTappMapper dmapper;

    @Autowired
    private DRefMapper dRefMapper;

    @Autowired
    private TusersMapper tusersMapper;
    @Autowired
    private TareaMapper tareaMapper;

    @Override
    public BaseMapper<MTapp> getMapper() {
        return mapper;
    }

    public int insertData(MTapp bean) throws Exception {
        int count = 0;
        MTarea mProvince = tareaMapper.single(bean.getPROVINCE_ID());
        MTarea mCity = tareaMapper.single(bean.getCITY_ID());
        bean.setCITY_IDNAME(mCity.getSNAME());
        bean.setPROVINCE_IDNAME(mProvince.getSNAME());

        count = getRecordCount(" AND SNAME='" + bean.getSNAME() + "'");
        if (count > 0)
            throw new Exception("应用名称已存在");
        count = tusersMapper.getRecordCount(" AND SNAME='" + bean.getUSERNAME() + "'");
        if (count > 0)
            throw new Exception("用户名称已存在");
        mapper.insert(bean);
        MTusers mTusers = new MTusers();
        mTusers.setSNAME(bean.getUSERNAME());
        mTusers.setAPP_ID(bean.getAPP_ID());
        mTusers.setBADMIN(1);
        mTusers.setPASSWORD("123");
        mTusers.setBDEL(bean.getBDEL());
        mTusers.setPROVINCE_ID(bean.getPROVINCE_ID());
        mTusers.setCITY_ID(bean.getCITY_ID());
        mTusers.setMEMO("生成一个用户");
        tusersMapper.insert(mTusers);
        return 1;
    }

    public void updateData(MTapp bean) throws Exception {
        int count = 0;
        MTapp old = single(bean.getAPP_ID());
        MTusers mTusers = tusersMapper.singleByWhere(" AND SNAME='" + old.getUSERNAME() + "' AND  BADMIN=1");
        if (mTusers != null) {
            count = getRecordCount(" AND SNAME='" + bean.getSNAME() + "' AND APP_ID!=" + bean.getAPP_ID() + "");
            if (count > 0)
                throw new Exception("用户名称已存在");
            count = tusersMapper.getRecordCount(" AND SNAME='" + bean.getUSERNAME() + "' AND USERS_ID!=" + mTusers.getUSERS_ID() + " ");
            if (count > 0)
                throw new Exception("用户名称已存在");
            mapper.update(bean);
            //MTusers mTusers1=new MTusers();
            mTusers.setPROVINCE_ID(bean.getPROVINCE_ID());
            mTusers.setCITY_ID(bean.getCITY_ID());
            mTusers.setSNAME(bean.getUSERNAME());
            mTusers.setBDEL(bean.getBDEL());
            tusersMapper.update(mTusers);
        } else {
            throw new Exception("用户数据丢失！");
        }

    }

    @Override
    public void deleteById(int id) {
        MTapp old = single(id);
        MTusers mTusers = tusersMapper.singleByWhere(" AND SNAME='" + old.getUSERNAME() + "' AND  BADMIN=1");
        mapper.deleteById(id);
        tusersMapper.deleteById(mTusers.getUSERS_ID());
    }


    @Override
    public List<Map<String, Object>> fetchByPage(Map<String, Object> map) {
        return dmapper.fetchByPage(map);
    }

    @Override
    public Map<String, Object> initEdit() {
        Map<String, Object> map = new HashMap<>();
        List<MRef> provinceList = dRefMapper.getProvince();
        map.put("province", provinceList);
        return map;
    }

    @Override
    public Map<String, Object> initList() {
        Map<String, Object> map = new HashMap<>();
        List<MRef> provinceList = dRefMapper.getProvince();
        map.put("province", provinceList);
        return map;
    }
}
